package cn.david.order.service.impl;


import cn.david.basic.service.impl.BasicServiceImpl;
import cn.david.basic.util.CodeGenerateUtils;
import cn.david.basic.util.DistanceUtil;
import cn.david.basic.util.Point;
import cn.david.order.domain.AdoptOrder;
import cn.david.order.domain.OrderAddress;
import cn.david.order.domain.ProductOrder;
import cn.david.order.domain.ProductOrderDetail;
import cn.david.order.mapper.OrderAddressMapper;
import cn.david.order.mapper.ProductOrderDetailMapper;
import cn.david.order.mapper.ProductOrderMapper;
import cn.david.order.service.IProductOrderService;
import cn.david.org.domain.Shop;
import cn.david.org.mapper.ShopMapper;
import cn.david.product.domain.Product;
import cn.david.product.mapper.ProductMapper;
import cn.david.user.domian.Logininfo;

import cn.david.user.domian.User;
import cn.david.user.domian.UserAddress;
import cn.david.user.mapper.UserAddressMapper;
import cn.david.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
public class ProductOrderServiceImpl extends BasicServiceImpl<ProductOrder> implements IProductOrderService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ProductOrderMapper productOrderMapper;
    @Autowired
    private ProductOrderDetailMapper productOrderDetailMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;



    @Override
    public String submit(Map<String, Object> params, Logininfo loginInfo) {
        //把字符串转成LONG类型
        Long productId = Long.valueOf(params.get("product_id").toString());
        Long addressId = Long.valueOf(params.get("address_id").toString());
        Integer payMethod = Integer.valueOf(params.get("pay_method").toString());
        Integer serviceMethod = Integer.valueOf(params.get("service_method").toString());
        Integer productNum = Integer.valueOf(params.get("product_num").toString());


        Product product = productMapper.loadById(productId);
        User user = userMapper.loadByLogininfoId(loginInfo.getId());
        UserAddress userAddress = userAddressMapper.loadById(addressId);
        //生成订单
        ProductOrder productOrder = initProductOrder(productId, productNum,  product, user, userAddress);
        productOrderMapper.save(productOrder);
        //订单地址
        ProductOrderDetail productOrderDetail = initProductOrderDetail(productId, product, productOrder);
        productOrderDetailMapper.save(productOrderDetail);
        //订单地址

        OrderAddress orderAddress = initOrderAddress(productOrder, userAddress);
        orderAddressMapper.save(orderAddress);

        return null;
    }
    private OrderAddress initOrderAddress(ProductOrder order, UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress, orderAddress);
        orderAddress.setId(null);
        orderAddress.setOrder_id(order.getId());
        orderAddress.setOrderSn(order.getOrderSn());
        return orderAddress;
    }

    private ProductOrderDetail initProductOrderDetail(Long productId, Product product, ProductOrder productOrder) {
        //订单地址
        ProductOrderDetail productOrderDetail = new ProductOrderDetail();
        BeanUtils.copyProperties(product, productOrderDetail);
        productOrderDetail.setId(null);
        productOrderDetail.setProduct_id(productId);
        productOrderDetail.setOrder_id(productOrder.getId());
        return productOrderDetail;
    }

    private ProductOrder initProductOrder(Long productId, Integer productNum, Product product, User user, UserAddress userAddress) {
        ProductOrder order = new ProductOrder();
        order.setDigest("[陶大大的摘要]"+product.getName());
        order.setPrice(product.getSaleprice().multiply(new BigDecimal(productNum)));
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        order.setLastConfirmTime(new Date(System.currentTimeMillis()+15*60*1000));
        order.setProduct_id(productId);
        order.setUser_id(user.getId());
        Point point = DistanceUtil.getPoint(userAddress.getFullAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
        order.setShop_id(nearestShop.getId());
        return order;
    }
}
